package com.copa.l2404;

import java.util.HashMap;
import java.util.Map;

/**
 * @author copa
 * @createDate 2023-04-13 8:39
 * @function hashmap法
 */
public class Test1 {

    public int mostFrequentEven(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (num % 2 == 0) {
                map.put(num, map.getOrDefault(num, 0) + 1);
            }
        }
        int maxNum = -1;
        int result = -1;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() > maxNum) {
                result = entry.getKey();
                maxNum = entry.getValue();
                continue;
            }
            if (entry.getValue() == maxNum && entry.getKey() < result) {
                result = entry.getKey();
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {0,1,2,2,4,4,1};
        System.out.println(new Test1().mostFrequentEven(nums));
    }
}
